package control
{
	import core.Control;
	import core.Model;
	import core.View;
	
	import events.LoginEvent;
	import events.TableEvent;
	
	import flash.utils.Dictionary;
	
	import global.GameState;
	
	import utils.DisplayTool;
	
	import view.panel.AbsPanel;
	
	public class SceneControl
	{
		private var _model:Model = Model.getInstance();
		private var _view:View = View.getInstance();
		private var _control:Control = Control.getInstance();
		
		private var openedPanels:Dictionary = new Dictionary(true);
		
		public function SceneControl()
		{
			initListener();	
			
		}
		
		private function initListener():void
		{
			_model.loginModel.addEventListener(LoginEvent.GAMESERVER_CONNETED,onLoginGameServerConnect);
			_model.loginModel.addEventListener(LoginEvent.GAMESERVER_LOGINING,onGameServerLogining);
			_model.tableModel.addEventListener(TableEvent.GO_TO_HALL,onGoHall);
		}
		
		private function onGoHall(event:TableEvent):void
		{
			switchToHall();
			_control.playerInfoModulControl.refreshInfo();
		}
		
		private function onLoginGameServerConnect(event:LoginEvent):void
		{
			switchToGame();
			_model.netRemote.sendGameLogin(GameState.snsId,GameState.playerName,GameState.snsKey,_model.loginModel.currentRoomId,GameState.sns,GameState.isQuickGame);
		}
		
		private function onGameServerLogining(event:LoginEvent):void
		{
			switchToGameServerLogining();
		}
		
		
		public function addOpenedPanel(panel:AbsPanel):void
		{
			openedPanels[panel] = panel;
		}
		
		public function removeOpenedPanel(panel:AbsPanel):void
		{
			openedPanels[panel] = null;
			delete openedPanels[panel];
		}
		
		public function closeAllOpenPanel():void
		{
			for each (var panel:AbsPanel in openedPanels)
			{
				panel.close();
				openedPanels[panel] = null;
				delete openedPanels[panel];
			}
		}
		
		public function switchToGame():void
		{
			GameState.isInGameRoom = true;
			removeAllLayer();
			_view.scene.addChild(_view.tableLayer);
			_view.scene.addChild(_view.rankModul);
			_view.scene.addChild(_view.panelLayer);
			_view.scene.addChild(_view.hintLayer);
			
			closeAllOpenPanel();
		}
		
		public function switchToGameServerLogining():void
		{
			removeAllLayer();
			
			_view.scene.addChild(_view.gameServerLoginingLayer);
			_view.scene.addChild(_view.rankModul);
			_view.scene.addChild(_view.panelLayer);
			_view.scene.addChild(_view.hintLayer);
			
			_view.gameServerLoginingLayer.setLogo();
		}
		
		public function lockWorld():void
		{
			_view.hallLayer.mouseChildren = false;
			_view.panelLayer.mouseChildren = false;
			//_view.hintLayer.mouseChildren = false;
			_view.tableLayer.mouseChildren = false;
			
			_view.hallLayer.mouseEnabled = false;
			_view.panelLayer.mouseEnabled = false;
			//_view.hintLayer.mouseEnabled = false;
			_view.tableLayer.mouseEnabled = false;
			
			drawWorldMask();
		}
		
		public function unlockWorld():void
		{
			_view.hallLayer.mouseChildren = true;
			_view.panelLayer.mouseChildren = true;
			//_view.hintLayer.mouseChildren = true;
			_view.tableLayer.mouseChildren = true;
			
			_view.hallLayer.mouseEnabled = true;
			_view.panelLayer.mouseEnabled = true;
			//_view.hintLayer.mouseEnabled = true;
			_view.tableLayer.mouseEnabled = true;
			
			_view.panelLayer.graphics.clear();
		}
		
		private function drawWorldMask():void
		{
			with (_view.panelLayer.graphics)
			{
				clear();
				beginFill(0,.5);
				drawRect(0,0,GameState.GAME_WIDTH,750);
				endFill();
			}
		}
		
		public function switchToHall():void
		{
			GameState.isInGameRoom = false;
			
			removeAllLayer();
			_view.scene.addChild(_view.hallLayer);
			_view.scene.addChild(_view.rankModul);
			_view.scene.addChild(_view.panelLayer);
			_view.scene.addChild(_view.hintLayer);
			
			_view.hallLayer.setLogo();
			
			_model.netSocket.close();
			_model.sitModel.clear();
			_model.playerModel.clear();
			_model.roleModel.clear();
			_model.friendModel.clear();
			_model.mailModel.clear();
			_model.tableModel.clear();
			_model.netRemote.clear();
			
			_control.soundControl.stopAllSound();
			_control.hintControl.clear();
			_control.emoteControl.clear();
			//_control.playerListControl.clear();
			_control.promptPanelSelectCombineCardsControl.clear();
			_control.serverPromptPanelControl.clear();
			_control.playPanelControl.clear();
			_control.resultPanelControl.clear();
			_control.sitControl.clear();
			_control.mailControl.clear();
			_control.tableFlyGoldControl.clear();
			_control.itemPanelControl.clear();
			
			closeAllOpenPanel();
		}
		
		private function removeAllLayer():void
		{
			DisplayTool.removeChild(_view.scene,_view.hallLayer);
			DisplayTool.removeChild(_view.scene,_view.panelLayer);
			DisplayTool.removeChild(_view.scene,_view.hintLayer);
			DisplayTool.removeChild(_view.scene,_view.tableLayer);
			DisplayTool.removeChild(_view.scene,_view.gameServerLoginingLayer);
			_view.gameServerLoginingLayer.clear();
		}
	}
}